package com.zzyl.nursing.controller;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.zzyl.nursing.dto.CancelDto;
import com.zzyl.nursing.dto.ExecuteTaskDto;
import com.zzyl.nursing.dto.NursingTaskDto;
import com.zzyl.nursing.vo.NursingTaskDetailVo;
import com.zzyl.nursing.vo.NursingTaskTimeUpdateDto;
import com.zzyl.nursing.vo.NursingTaskVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.NursingTask;
import com.zzyl.nursing.service.INursingTaskService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 护理任务Controller
 *
 * @author zhangyan
 * @date 2025-06-05
 */
@RestController
@RequestMapping("/nursing/nursingTask")
@Api(tags = "护理任务的接口")
public class NursingTaskController extends BaseController {
    @Autowired
    private INursingTaskService nursingTaskService;

    /**
     * 查询护理任务列表
     */
    @ApiOperation("查询护理任务列表")
    @PreAuthorize("@ss.hasPermi('nursing:nursingTask:list')")
    @GetMapping("/list")
    public TableDataInfo list(NursingTaskDto dto) {
        startPage();
        List<NursingTask> list = nursingTaskService.selectNursingTaskList(dto);
        List<NursingTaskVo> listVo = list.stream().map(nursingTask -> {
            NursingTaskVo nursingTaskVo = BeanUtil.toBean(nursingTask, NursingTaskVo.class);
            String nameStr = nursingTask.getNursingName();
            if (StrUtil.isNotEmpty(nameStr)) {
                nursingTaskVo.setNursingName(Arrays.asList(nameStr.split(",")));
            } else {
                nursingTaskVo.setNursingName(Collections.emptyList());
            }
            return nursingTaskVo;
        }).collect(Collectors.toList());
        return getDataTable(listVo,list);
    }

//    /**
//     * 导出护理任务列表
//     */
//    @ApiOperation("导出护理任务列表")
//    @PreAuthorize("@ss.hasPermi('nursing:nursingTask:export')")
//    @Log(title = "护理任务", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, NursingTask nursingTask) {
//        List<NursingTask> list = nursingTaskService.selectNursingTaskList(nursingTask);
//        ExcelUtil<NursingTask> util = new ExcelUtil<NursingTask>(NursingTask.class);
//        util.exportExcel(response, list, "护理任务数据");
//    }

    /**
     * 获取护理任务详细信息
     */
    @ApiOperation("获取护理任务详细信息")
    @PreAuthorize("@ss.hasPermi('nursing:nursingTask:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@ApiParam(value = "护理任务ID", required = true)
                              @PathVariable("id") Long id) {
        NursingTaskDetailVo nursingTaskDetailVo = nursingTaskService.selectNursingTaskById(id);
        return success(nursingTaskDetailVo);
    }

    /**
     * 新增护理任务
     */
    @ApiOperation("新增护理任务")
    @PreAuthorize("@ss.hasPermi('nursing:nursingTask:add')")
    @Log(title = "护理任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@ApiParam(value = "护理任务实体")
                          @RequestBody NursingTask nursingTask) {
        return toAjax(nursingTaskService.insertNursingTask(nursingTask));
    }

    /**
     * 取消任务
     */
    @ApiOperation("取消任务实体")
    @PreAuthorize("@ss.hasPermi('nursing:nursingTask:edit')")
    @Log(title = "护理任务", businessType = BusinessType.UPDATE)
    @PutMapping("/cancel")
    public AjaxResult cancel(@ApiParam(value = "取消任务实体")
                           @RequestBody CancelDto cancelDto) {
        nursingTaskService.cancel(cancelDto);
        return success();
    }

    /**
     * 执行任务
     */
    @ApiOperation("执行任务实体")
    @PreAuthorize("@ss.hasPermi('nursing:nursingTask:do')")
    @Log(title = "护理任务", businessType = BusinessType.UPDATE)
    @PutMapping("/do")
    public AjaxResult performTask(@ApiParam(value = "执行任务实体")
                             @RequestBody ExecuteTaskDto executeTaskDto) {
        nursingTaskService.performTask(executeTaskDto);
        return success();
    }

    /**
     * 任务改期
     */
    @ApiOperation("任务改期")
    @PreAuthorize("@ss.hasPermi('nursing:nursingTask:editTime')")
    @Log(title = "任务改期", businessType = BusinessType.UPDATE)
    @PutMapping("/updateTime")
    public AjaxResult updateTime(@ApiParam(value = "任务改期实体")
                                  @RequestBody NursingTaskTimeUpdateDto dto) {
        nursingTaskService.updateTime(dto);
        return success();
    }

    /**
     * 修改护理任务
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('nursing:nursingTask:edit')")
    @Log(title = "护理任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@ApiParam(value = "护理任务实体")
                           @RequestBody NursingTask nursingTask) {
        return toAjax(nursingTaskService.updateNursingTask(nursingTask));
    }

    /**
     * 删除护理任务
     */
    @ApiOperation("删除护理任务")
    @PreAuthorize("@ss.hasPermi('nursing:nursingTask:remove')")
    @Log(title = "护理任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(nursingTaskService.deleteNursingTaskByIds(ids));
    }

}
